package com.longqinuo.mycarservice.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;


import com.longqinuo.mycarservice.R;
import com.longqinuo.mycarservice.entity.CarFriend;
import com.longqinuo.mycarservice.view.CircleNetworkImage;


import java.util.List;

/**
 * 自定义快递电话列表适配器
 * Created by zxw on 2016/6/8.
 */
public class MyFriendAdapter extends BaseAdapter {
    private Context context;
    private List<CarFriend> mList;
    private ImageLoader mImageLoader;
    public MyFriendAdapter(Context context, List<CarFriend> list,ImageLoader imageLoader) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.mList = list;
        this.mImageLoader = imageLoader;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return mList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.car_friend_item_layout, null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CarFriend c = mList.get(position);
        holder.header.setImageUrl(c.getHeader(),mImageLoader);
        holder.name.setText(c.getName());
        holder.time.setText(c.getTime());
        holder.content.setText(c.getContent());

        List<String> images = c.getImages();
        holder.image.setAdapter(new NineGridImageViewAdapter() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, Object o) {
                Log.d("yz","onDisplayImage");
                NetworkImageView networkImageView = (NetworkImageView) imageView;
                networkImageView.setImageUrl((String)o,mImageLoader);
            }

            @Override
            protected void onItemImageClick(Context context, int index, List list) {
                super.onItemImageClick(context, index, list);
            }

            @Override
            protected ImageView generateImageView(Context context) {
                NetworkImageView imageView = new NetworkImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                return imageView;
            }
        });
        holder.image.setImagesData(images);

        holder.lookCount.setText(c.getLookCount());
        boolean isLike = c.isLike();
        if (isLike) {
            holder.goodLike.setImageResource(R.mipmap.good_like_blue);
        } else {
            holder.goodLike.setImageResource(R.mipmap.good_like_grey);
        }
        holder.likeCount.setText(c.getGoodCount());

        return convertView;
    }

    public class ViewHolder{

        public ViewHolder(View itemView) {
            this.header = (CircleNetworkImage) itemView.findViewById(R.id.iv_car_friend_header);
            this.name = (TextView) itemView.findViewById(R.id.tv_car_friend_name);
            this.time = (TextView) itemView.findViewById(R.id.tv_car_friend_time);
            this.content = (TextView) itemView.findViewById(R.id.tv_car_friend_content);
            this.image = (NineGridImageView) itemView.findViewById(R.id.gv_car_friend_image);
            this.lookCount = (TextView) itemView.findViewById(R.id.tv_look_count_friend);
            this.goodLike = (ImageView) itemView.findViewById(R.id.iv_good_count_friend);
            this.likeCount = (TextView) itemView.findViewById(R.id.tv_good_count_friend);
        }

        private CircleNetworkImage header;
        private TextView name;
        private TextView time;
        private TextView content;
        private NineGridImageView image;
        private TextView lookCount;
        private ImageView goodLike;
        private TextView likeCount;
    }
}
